package com.example.project.service.impl;

import com.example.project.dao.TcdmSecureHazardListRecycleDao;
import com.example.project.entity.InventoryTrend;
import com.example.project.entity.Pie;
import com.example.project.entity.TcdmSecureHazardList;
import com.example.project.dao.TcdmSecureHazardListDao;
import com.example.project.entity.TcdmSecureHazardListRecycle;
import com.example.project.service.TcdmSecureHazardListService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * (TcdmSecureHazardList)表服务实现类
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@Service("tcdmSecureHazardListService")
public class TcdmSecureHazardListServiceImpl implements TcdmSecureHazardListService {
    @Resource
    private TcdmSecureHazardListDao tcdmSecureHazardListDao;

    @Resource
    private TcdmSecureHazardListRecycleDao tcdmSecureHazardListRecycleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param tshlId 主键
     * @return 实例对象
     */
    @Override
    public TcdmSecureHazardList queryById(Integer tshlId) {
        return this.tcdmSecureHazardListDao.queryById(tshlId);
    }

    @Override
    public List<TcdmSecureHazardList> findList() {
        return this.tcdmSecureHazardListDao.findList();
    }

    /**
     * 分页查询
     *
     * @param tcdmSecureHazardList 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TcdmSecureHazardList> queryByPage(TcdmSecureHazardList tcdmSecureHazardList, PageRequest pageRequest,Date star,Date end) {
        long total = this.tcdmSecureHazardListDao.count(tcdmSecureHazardList);
        return new PageImpl<>(this.tcdmSecureHazardListDao.queryAllByLimit(tcdmSecureHazardList, pageRequest,star,end), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param tcdmSecureHazardList 实例对象
     * @return 实例对象
     */
    @Override
    public TcdmSecureHazardList insert(TcdmSecureHazardList tcdmSecureHazardList) {
        //将回收站状态设置为1，考核状态设置为0,如果创建时间为空，新增时间
        if (tcdmSecureHazardList.getTshlUt()==null){
            tcdmSecureHazardList.setTshlUt(new Date());
        }
        tcdmSecureHazardList.setValid(1);
        tcdmSecureHazardList.setIscheck(0);
        this.tcdmSecureHazardListDao.insert(tcdmSecureHazardList);
        return tcdmSecureHazardList;
    }

    /**
     * 修改数据
     *
     * @param tcdmSecureHazardList 实例对象
     * @return 实例对象
     */
    @Override
    public TcdmSecureHazardList update(TcdmSecureHazardList tcdmSecureHazardList) {
        this.tcdmSecureHazardListDao.update(tcdmSecureHazardList);
        return this.queryById(tcdmSecureHazardList.getTshlId());
    }

    /**
     * 通过主键删除数据
     *
     * @param tshlId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer tshlId) {
        return this.tcdmSecureHazardListDao.deleteById(tshlId) > 0;
    }

    @Override
    public boolean deleteById1(Integer id) {
       TcdmSecureHazardList tcdmSecureHazardList=this.tcdmSecureHazardListDao.queryById(id);
       if (tcdmSecureHazardList!=null){
           tcdmSecureHazardList.setValid(0);
           TcdmSecureHazardListRecycle tcdmSecureHazardListRecycle=new TcdmSecureHazardListRecycle();
           tcdmSecureHazardListRecycle.setTshlrUt(new Date());
           tcdmSecureHazardListRecycle.setTshaId(id);
           tcdmSecureHazardListRecycle.setTshlName(tcdmSecureHazardList.getTshlName());
           tcdmSecureHazardListRecycle.setTshlUnit(tcdmSecureHazardList.getTshlUnit());
           tcdmSecureHazardListRecycle.setTshlGrade(tcdmSecureHazardList.getTshlClassify());
           this.tcdmSecureHazardListRecycleDao.insert(tcdmSecureHazardListRecycle);
           return this.tcdmSecureHazardListDao.update(tcdmSecureHazardList)>0;
       }
       else return false;
    }

    @Override
    public boolean deleteByValid() {
        return this.tcdmSecureHazardListDao.deleteByValid();
    }

    @Override
    public InventoryTrend getTrend(TcdmSecureHazardList tcdmSecureHazardList,Date star,Date end) throws ParseException {
//        Date endTime=new Date();
        //如果前端未传时间则寻找最近七天的隐患数据，如果传入就查找相应的数据

        List<String> times=new ArrayList<>();
        List<Integer> counts=new ArrayList<>();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        if (star==null){
            for (int i=0;i<7;i++){
                Calendar calendar=Calendar.getInstance();
                calendar.setTime(new Date());
                calendar.add(Calendar.DATE,-(6-i));
                Date endTime=calendar.getTime();
                String s=format.format(endTime);
                endTime=format.parse(s);
                //获取隐患开始和处理时间，如果在这之间则将当前隐患数量加1
                List<TcdmSecureHazardList> alldata=this.tcdmSecureHazardListDao.queryByCondition(tcdmSecureHazardList);
                int count=0;
                times.add(s);
                for (int j=0;j<alldata.size();j++){
                    TcdmSecureHazardList item=alldata.get(j);
                    item.setEndTimes(endTime);
                    this.tcdmSecureHazardListDao.update(item);
                    if (this.tcdmSecureHazardListDao.getTrendCount(item)!=null){
                        count++;
                    }
                }
                counts.add(count);
            }

        }else{
           Calendar calendar2=Calendar.getInstance();
            calendar2.setTime(end);
            calendar2.add(Calendar.DATE,0);
            Date endding=calendar2.getTime();
            String si=format.format(endding);
            endding=format.parse(si);
            Calendar calendar=Calendar.getInstance();
            calendar.setTime(star);
            calendar.add(Calendar.DATE,0);
            Date endTime=calendar.getTime();
            String s=format.format(endTime);
            endTime=format.parse(s);
            while (true){
                Calendar calendar3=Calendar.getInstance();
                calendar.setTime(endTime);
                calendar.add(Calendar.DATE,+1);
                endTime=calendar.getTime();
                String s2=format.format(endTime);
                endTime=format.parse(s2);
                //获取隐患开始和处理时间，如果在这之间则将当前隐患数量加1
                List<TcdmSecureHazardList> alldata=this.tcdmSecureHazardListDao.queryByCondition(tcdmSecureHazardList);
                int count=0;
                times.add(s);
                for (int j=0;j<alldata.size();j++){
                    TcdmSecureHazardList item=alldata.get(j);
                    item.setEndTimes(endTime);
                    this.tcdmSecureHazardListDao.update(item);
                    if (this.tcdmSecureHazardListDao.getTrendCount(item)!=null){
                        count++;
                    }
                }
                counts.add(count);
                if (si.equals(s2)){
                    break;
                }
            }
        }
        List<Pie> pie=new ArrayList<>();
        if (tcdmSecureHazardList.getTshlStat()==null){
            Pie pie1=new Pie();
            pie1.setName("已处理");
            tcdmSecureHazardList.setTshlStat("4");
            pie1.setValue(this.tcdmSecureHazardListDao.queryByState(tcdmSecureHazardList));
            pie.add(pie1);
            Pie pie2=new Pie();
            pie2.setName("处理中");
            tcdmSecureHazardList.setTshlStat("6");
            pie2.setValue(this.tcdmSecureHazardListDao.queryByState(tcdmSecureHazardList));
            pie.add(pie2);
            Pie pie3=new Pie();
            pie3.setName("挂起中");
            tcdmSecureHazardList.setTshlStat("5");
            pie3.setValue(this.tcdmSecureHazardListDao.queryByState(tcdmSecureHazardList));
            pie.add(pie3);
        }
        else{
            Pie pie1=new Pie();
            if (tcdmSecureHazardList.getTshlStat()=="1"){
                pie1.setName("待指派");
            }
            else if (tcdmSecureHazardList.getTshlStat()=="2"){
                pie1.setName("未处理");
            }
            else if (tcdmSecureHazardList.getTshlStat()=="3"){
                pie1.setName("待验收");
            }
           else if (tcdmSecureHazardList.getTshlStat()=="4"){
                pie1.setName("已处理");
            }
           else if (tcdmSecureHazardList.getTshlStat()=="5"){
                pie1.setName("挂起中");
            }
           else if (tcdmSecureHazardList.getTshlStat()=="6"){
                pie1.setName("处理中");
            }
            pie1.setValue(this.tcdmSecureHazardListDao.queryByState(tcdmSecureHazardList));
            pie.add(pie1);
        }

//       Integer count=this.tcdmSecureHazardListDao.getTrendCount(tcdmSecureHazardList,endTime);
        return new InventoryTrend(times,counts,pie);
    }
}
